package cn.main.controller;

import cn.main.core.SessionHelper;
import cn.main.core.UserSession;
import cn.main.entity.sys.SysColumn;
import cn.main.entity.sys.SysFunction;
import cn.main.entity.sys.User;
import cn.main.service.FunctionService;
import com.mysql.jdbc.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URL;
import java.util.List;

/**
 * Author: chengwei
 * Date:   2016/7/13
 * Time:   11:21
 */
@Controller
@RequestMapping("/user/function")
public class FunctionController {
    @Resource
    private FunctionService service;

    @RequestMapping("/save")
    @ResponseBody
    public String saveOrUpdateFunction(SysFunction function, HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        service.saveOrUpdateFunction(session,function);
        return null;
    }

    @RequestMapping("/delete")
    public void deleteFunction(@RequestParam("function_id") String functionId,HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        service.deleteFunction(session,functionId);
    }

    @RequestMapping("/find")
    @ResponseBody
    public SysFunction findFunctionById(@RequestParam("function_id") String functionId,HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        return service.findFunctionById(session,functionId);
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<SysFunction> getFunctionList(HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        return service.getFunctionList(session);
    }

    @RequestMapping("/open")
    public ModelAndView openFunction(@RequestParam("function_id") String functionId, HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        SysFunction function = service.findFunctionById(session,functionId);
        if (function == null) {
            return new ModelAndView("redirect:/login");
        }
        String url = function.getUrl();
        if (!function.isJsp()) {
            url = "redirect:" + url;
        }
        return new ModelAndView(url);
    }

    @RequestMapping("/show")
    @ResponseBody
    public List<SysFunction> getFunctionList(@RequestParam("column_id")String columnId,HttpServletRequest request) throws Exception {
//        UserSession session = SessionHelper.getUserSession(request);
        return service.getFunctionList(columnId);
    }

    @RequestMapping("/form")
    public ModelAndView formFunctionById(HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        String functionId = request.getParameter("function_id");
        SysFunction function;
        if (!StringUtils.isNullOrEmpty(functionId)) {
            function = service.findFunctionById(session,functionId);
        } else {
            function = new SysFunction();
        }

        return new ModelAndView("/sys/function/formFunction","function",function);
    }
}
